<template>
  <div>
    <!-- center	是否对头部和底部采用居中布局 -->
    <el-dialog
      center
      :title="info.isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
    >
      <el-form :model="menuForm" :rules="rules" ref="menu">
        <el-form-item
          prop="title"
          label="菜单名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="menuForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="menuForm.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menuList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuForm.type" :label="1">目录</el-radio>
          <el-radio v-model="menuForm.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标" :label-width="formLabelWidth">
          <el-input v-model="menuForm.icon" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单地址" :label-width="formLabelWidth">
          <el-input v-model="menuForm.url" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="submitInfo"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="submitInfo">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的接口
import { menuList, menuAdd, menuEdit } from "../../util/request/api";
export default {
  props: ["info"],
  data() {
    return {
      // 表单校验的规则
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          {
            min: 2,
            max: 8,
            message: "长度在 2 到 8 个字符",
            trigger: "blur",
          },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "blur" }],
      },
      formLabelWidth: "120px",
      // 下拉菜单的数据
      menuList: [],
      // 表单的数据
      menuForm: {
        pid: 0, //上级菜单
        title: "", //菜单名称
        icon: "", //图标
        type: 1, //类型 1目录2菜单
        url: "", //菜单地址
        status: 1, //状态   1正常2禁用   number类型
      },
    };
  },
  mounted() {
    //页面一加载就调用接口
    this.getMenu();
  },
  methods: {
    // 设置表单中的值,给menuForm赋值
    setVal(data) {
      // 解构，为了避免数据互相影响
      this.menuForm = { ...data };
    },
    //封装一个获取接口列表的函数
    getMenu() {
      menuList({
        istree: true,
      }).then((res) => {
        console.log(res, "菜单列表");
        if (res.code == 200) {
          this.menuList = res.list;
        }
      });
    },
    //重置事件
    reset() {
      this.menuForm = {
        pid: 0, //上级菜单
        title: "", //菜单名称
        icon: "", //图标
        type: 1, //类型 1目录2菜单
        url: "", //菜单地址
        status: 1, //状态   1正常2禁用   number类型
      };
    },
    //封装一个取消（关闭弹框）事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //封装一提交事件（包含添加和修改）
    submitInfo() {
      // $refs.menu 查找获取dom节点或者组件
      // validate 校验表单
      this.$refs.menu.validate(async (val) => {
        // 判断数据中是否有children，后台不需要，所以删掉
        this.menuForm.children ? delete this.menuForm.children : "";
        if (val) {
          let res = {};
          // 判断新增或修改
          if (this.info.isAdd) {
            res = await menuAdd(this.menuForm);
          } else {
            res = await menuEdit(this.menuForm);
          }
          if (res.code == 200) {
            this.$message.success(res.msg);
            // 通知父组件刷新数据
            this.$emit("update");
            //关闭弹框
            this.cancel();
          } else {
            this.$message.error(res.msg);
          }
        } else {
          console.log(val, "22222");
        }
      });
    },
  },
};
</script>

<style scoped></style>
